package com.qiao.glod.ui.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.TextViewCompat;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.qiao.glod.R;
import com.qiao.glod.adapter.QuickAdapter;
import com.qiao.glod.view.item.MenuHeader;
import com.qiao.glod.view.item.MenuItem;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Qiao on 2016/4/7.
 * function：
 */
public class DrawerFragment extends ListFragment{
    private MenuHeader mMenuHeader;
    private int mIconSize;
    private OnDrawerItemChangedListener onDrawerItemChangedListener;
    private int mLastCheckedItemIndex=0;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(activity instanceof OnDrawerItemChangedListener){
            onDrawerItemChangedListener = (OnDrawerItemChangedListener)activity;
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ListView listView = getListView();
        listView.setBackgroundColor(Color.WHITE);
        listView.setSelector(new ColorDrawable(0));
        listView.setCacheColorHint(Color.TRANSPARENT);
        listView.setDivider(null);
        listView.setOverScrollMode(AbsListView.OVER_SCROLL_NEVER);
        listView.setHorizontalScrollBarEnabled(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        listView.addHeaderView(mMenuHeader =new MenuHeader(getContext()));

        mIconSize = getResources().getDimensionPixelSize(R.dimen.drawer_icon_size);
        resolveBaseColror();
        setListAdapter(mAdapter);
    }

    private List<MenuItem> generateMenuItems(){
        return Arrays.asList(
                new MenuItem("camera", R.drawable.ic_menu_camera, MainFragment.class.getName()),
                new MenuItem("gallery", R.drawable.ic_menu_gallery, TabFragment.class.getName()),
                new MenuItem("send", R.drawable.ic_menu_send, TFragment.class.getName()),
                new MenuItem("sledshow", R.drawable.ic_menu_slideshow, ContentFragment2.class.getName()),
                new MenuItem(),
                new MenuItem("More"),
                new MenuItem("share", R.drawable.ic_menu_share, ChoiceFragment.class.getName()),
                new MenuItem("manage", R.drawable.ic_menu_manage, MainFragment.class.getName())
        );
    }

    private QuickAdapter<MenuItem> mAdapter = new QuickAdapter<MenuItem>(generateMenuItems()) {

        @Override
        public int getViewTypeCount() {
            return 3;
        }

        @Override
        public int getItemViewType(int position) {
            return mData.get(position).getType();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent){
            int itemType = getItemViewType(position);
            final MenuItem data = getData(position);
            switch (itemType) {
                case MenuItem.ITEM_TYPE_MENU:
                    if(convertView==null) {
                        convertView = LayoutInflater.from(getContext())
                                .inflate(R.layout.menu_item, parent, false);
                    }

                    TextView itemView = (TextView) convertView;
                    itemView.setText(data.getText());
                    Drawable icon = getResources().getDrawable(data.getResId());

                    if(position == mLastCheckedItemIndex) {
                        itemView.setTextColor(checkedColor);
                        icon.setColorFilter(checkedColor, PorterDuff.Mode.SRC_IN);
                    }else{
                        itemView.setTextColor(baseColor);
                        icon.setColorFilter(baseColor, PorterDuff.Mode.MULTIPLY);
                    }

                    if (icon != null) {
                        icon.setBounds(0, 0, mIconSize, mIconSize);
                        TextViewCompat.setCompoundDrawablesRelative(itemView, icon, null, null, null);
                    }
                    break;
                case MenuItem.ITEM_TYPE_TITLE:
                    if(convertView==null) {
                        convertView = LayoutInflater.from(getContext())
                                .inflate(R.layout.menu_item_title, parent, false);
                    }

                    TextView title = (TextView) convertView;
                    title.setText(data.getText());
                    break;
                case MenuItem.ITEM_TYPE_SEPARATOR:
                    if(convertView==null) {
                        convertView = LayoutInflater.from(getContext())
                                .inflate(R.layout.menu_item_separator, parent, false);
                    }
                    break;
            }
            return convertView;
        }
    };

    private int baseColor,checkedColor;
    private void resolveBaseColror() {
        checkedColor = getResources().getColor(R.color.colorPrimary);
        int color = android.R.attr.textColorSecondary;
        TypedValue value = new TypedValue();
        if (!getContext().getTheme().resolveAttribute(color, value, true)) {
            return;
        }
        baseColor = getResources().getColor(value.resourceId);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        if(position == 0) return;//headerView 为第0个
        position--;

        mAdapter.notifyDataSetInvalidated();
        MenuItem data = mAdapter.getData(position);
        if(data.getType() == MenuItem.ITEM_TYPE_MENU){
            if(mLastCheckedItemIndex == position) return;

            mLastCheckedItemIndex = position;

            if(onDrawerItemChangedListener!=null){
                onDrawerItemChangedListener.onDrawerItemChanged(mLastCheckedItemIndex,data);
            }
        }
    }

    public interface OnDrawerItemChangedListener{
        void onDrawerItemChanged(int positon,MenuItem menu);
    }
}
